//
//  CCImageView.m
//  SelfDriving
//
//  Created by apple on 2017/1/25.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "CCImageView.h"

@implementation CCImageView
{
    /* 网络 */
    NSURLConnection *_conn;
    NSMutableData *_imageData;
    BOOL _isLoadEnded;
    
    /* 缩放--移动 */
    CGSize _originSize;
    
    double _startDisc;
    CGFloat _currentScale;
    
    CGPoint _originPoint;  //记录开始移动位置，有可能_imageLayer移除_baseLayer之外（如果之外需反弹回来）
    CGPoint _startPoint;   //开始移动位置
}
- (id)initWithFrame:(CGRect)frame img:(UIImageView *)img style:(CCImageViewStyle)style;
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
//        _imageUrl = url;
//        _style = style;
//        
//        _isLoadEnded = NO;
//        _currentScale = 1.0f;
//        self.backgroundColor = [UIColor lightGrayColor];
//        _baseLayer = [CAScrollLayer layer];
//        _baseLayer.frame = self.bounds;
//        _baseLayer.scrollMode = kCAScrollBoth;
//        _baseLayer.masksToBounds = YES;
//        [self.layer addSublayer:_baseLayer];
//        
//        //图片层
//        _imageLayer = [CALayer layer];
//        _imageLayer.frame = self.bounds;
//        _imageLayer.backgroundColor = [UIColor clearColor].CGColor;
//        //加载网络时提示
//        _indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: _style == CCImageViewStyleLarge ? UIActivityIndicatorViewStyleWhiteLarge : UIActivityIndicatorViewStyleWhite];
//        _indicatorView.frame = _style == CCImageViewStyleLarge ? CGRectMake((_imageLayer.frame.size.width - 37) / 2.0, (_imageLayer.frame.size.height - 37) / 2.0 - 20, 37, 37) : CGRectMake((_imageLayer.frame.size.width - 20) / 2.0, (_imageLayer.frame.size.height - 20) / 2.0 - 10, 20, 20);
//        [_indicatorView startAnimating];
//        [_imageLayer addSublayer:_indicatorView.layer];
//        
//        _toastLayer = [CATextLayer layer];
//        _toastLayer.string = @"加载中...";
//        _toastLayer.fontSize = _style == CCImageViewStyleLarge ? 16 : 14;
//        _toastLayer.frame = CGRectMake((_imageLayer.frame.size.width - 60)/2.0, (_imageLayer.frame.size.height - 50)/2.0 + 25, 60, 20);
//        [_imageLayer addSublayer:_toastLayer];
//        
//        [_baseLayer addSublayer:_imageLayer];
        self = img;
    }
    return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
        NSArray *arrayTouches = [[event allTouches] allObjects];
        if (arrayTouches.count == 1) {
            //记录开始点击的位置
            _startPoint = [[arrayTouches objectAtIndex:0] locationInView:self];
            _originPoint = _startPoint;
        }else if (arrayTouches.count > 1) {
            //如果是双指 记录两指之间的距离，方便后续判断是缩还是放
            CGPoint point1 = [[arrayTouches objectAtIndex:0] locationInView:self];
            CGPoint point2 = [[arrayTouches objectAtIndex:1] locationInView:self];
            _startDisc = sqrt((point1.x - point2.x)*(point1.x - point2.x) + (point1.y - point2.y)*(point1.y - point2.y));
        }
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesCancelled:touches withEvent:event];
    _startDisc = 0;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];
        NSArray *arrayTouches = [[event allTouches] allObjects];
        if (arrayTouches.count == 1) {
            //单指移动
            CGPoint tempPoint = [[arrayTouches objectAtIndex:0] locationInView:self];
            CGFloat moveX = tempPoint.x - _startPoint.x;
            CGFloat moveY = tempPoint.y - _startPoint.y;
            CGPoint oringinPoint = _imageLayer.position;
            //移动距离大于30 才执行移动操作
            if (fabs(moveX) > 30 || fabs(moveY) > 30) {
                _imageLayer.position = CGPointMake(oringinPoint.x + moveX, oringinPoint.y + moveY);
                _startPoint = tempPoint;
            }
        }else if (arrayTouches.count > 1) {
            //双指缩放
            CGPoint point1 = [[arrayTouches objectAtIndex:0] locationInView:self];
            CGPoint point2 = [[arrayTouches objectAtIndex:1] locationInView:self];
            double tempDisc = sqrt((point1.x - point2.x)*(point1.x - point2.x) + (point1.y - point2.y)*(point1.y - point2.y));
            //如果在touchesBegan时是单指，需重新赋值初始化距离
            if (_startDisc == 0) {
                _startDisc = tempDisc;
            }
            //计算缩放比例，判断最大缩放因子
            _currentScale += (tempDisc - _startDisc) / 100.0;
            if (_currentScale < MAX_SCALE + 0.5) {
                _imageLayer.bounds = CGRectMake(0, 0, _currentScale * _originSize.width, _currentScale * _originSize.height);
                _startDisc = tempDisc;
            }
        }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
    _startDisc = 0;
        NSArray *arrayTouches = [[event allTouches] allObjects];
        if (arrayTouches.count == 1) {
            //结束移动后，需要判断图片是否移动出屏幕，需重新设置其位置
            CGPoint endPoint = [[arrayTouches objectAtIndex:0] locationInView:self];
            CGFloat moveX = endPoint.x - _originPoint.x;
            CGFloat moveY = endPoint.y - _originPoint.y;
            if (moveX > 0 && _imageLayer.frame.origin.x > 0) {
                _imageLayer.frame = CGRectMake(0, _imageLayer.frame.origin.y, _imageLayer.frame.size.width, _imageLayer.frame.size.height);
            }
            if (moveX < 0 && _imageLayer.frame.origin.x + _imageLayer.frame.size.width < _baseLayer.frame.size.width) {
                _imageLayer.frame = CGRectMake(_baseLayer.frame.size.width - _imageLayer.frame.size.width, _imageLayer.frame.origin.y, _imageLayer.frame.size.width, _imageLayer.frame.size.height);
            }
            if (_imageLayer.frame.size.height > _baseLayer.frame.size.height) {
                if (moveY < 0 && _imageLayer.frame.origin.y + _imageLayer.frame.size.height < _baseLayer.frame.size.height) {
                    _imageLayer.frame = CGRectMake(_imageLayer.frame.origin.x, _baseLayer.frame.size.height - _imageLayer.frame.size.height, _imageLayer.frame.size.width, _imageLayer.frame.size.height);
                }
                if (moveY > 0 && _imageLayer.frame.origin.y > 0) {
                    _imageLayer.frame = CGRectMake(_imageLayer.frame.origin.x, 0, _imageLayer.frame.size.width, _imageLayer.frame.size.height);
                }
            } else {
                if (moveY > 0 && _imageLayer.frame.origin.y + _imageLayer.frame.size.height > _baseLayer.frame.size.height) {
                    _imageLayer.frame = CGRectMake(_imageLayer.frame.origin.x, _baseLayer.frame.size.height - _imageLayer.frame.size.height, _imageLayer.frame.size.width, _imageLayer.frame.size.height);
                }
                if (moveY < 0 && _imageLayer.frame.origin.y < 0) {
                    _imageLayer.frame = CGRectMake(_imageLayer.frame.origin.x, 0, _imageLayer.frame.size.width, _imageLayer.frame.size.height);
                }
            }
        }
        //缩放比例判断，超过最大或低于最小缩放比例
        if (_currentScale > MAX_SCALE) {
            _imageLayer.bounds = CGRectMake(0, 0, _originSize.width * MAX_SCALE, _originSize.height * MAX_SCALE);
            _currentScale = MAX_SCALE;
        }else if (_currentScale < MIN_SCALE) {
            _imageLayer.bounds = CGRectMake(0, 0, _originSize.width, _originSize.height);
            _currentScale = MIN_SCALE;
        }
        _startDisc = 0;
}
@end
